package com.jmpt.fangbohui.utils;

import com.jmpt.fangbohui.Constants;

import android.content.Context;
import android.content.SharedPreferences;


/**
 * @包名: com.hxjr.mbcbtoc.utils
 * @作者: Victor
 * @创建时间 : 2015/7/11 9:15
 * @描述: SharedPreferences 工具类
 */
public class SpUtils {

    private static SharedPreferences sharedPreferences;

    private static SharedPreferences.Editor edit;

    public static SharedPreferences getDefultSharedPreferences() {
        if (sharedPreferences == null) {
            sharedPreferences = UIUtils.getContext().getSharedPreferences(Constants.SharedPreferences.NAME, Context.MODE_PRIVATE);
        }
        return sharedPreferences;
    }
    
    public static SharedPreferences getSharedPreferences(String spName) {
        return UIUtils.getContext().getSharedPreferences(spName, Context.MODE_PRIVATE);
    }

    public static SharedPreferences.Editor getEdit() {
        if (edit == null) {
            edit = getDefultSharedPreferences().edit();
        }
        return edit;
    }

    public static String getString(String key, String defValue){
        return getDefultSharedPreferences().getString(key,defValue);
    }


    public static void putString(String key, String value){
        putString(key, value, true);
    }

    public static void putString(String key, String value, boolean autoClose){
        getEdit().putString(key, value);
        if(autoClose){
            close();
        }
    }

    public static void close(){
        getEdit().commit();
        getEdit().clear();
    }

}
